//
//  VistorLoginView.swift
//  sinaWeibo
//
//  Created by apple on 15/11/11.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

protocol VistorLoginViewDelegade: NSObjectProtocol {
    //注册代理方法
    func visitorWillLogin()
    //登陆代理方法
    func visitorWillRegister()
    
}

class VistorLoginView: UIView {
    //声明代理属性
    weak var VistorDelegade: VistorLoginViewDelegade?
    //点击了登陆调用的方法
    @objc func LogInButClick(){
        VistorDelegade?.visitorWillLogin()
    
    }
    //点击了注册调用的方法
    @objc func registerButClick(){
        VistorDelegade?.visitorWillRegister()
    
    }
    
    
   //设置页面信息
    func setupUIInfo(imageNmae:String?, title:String){
        //让其可见
        iconView.hidden = false
        tipLable.text = title
        if imageNmae != nil {
            circleView.image = UIImage(named: imageNmae!)
            //让iconView不可见
            iconView.hidden = true
        }else{
        //让 circleView转动起来
            startAnimation()
        }
    
    }
    //旋转动画方法
    private func startAnimation() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //让一直转
        anim.repeatCount = MAXFLOAT
        //转360度
        anim.toValue = 2 * M_PI
        //待续时间 秒
        anim.duration = 20
        //当动画结束或者视图处于非活跃状态 ，动画不移除图层
        anim.removedOnCompletion = false
       //把动画添加到 layer上
        circleView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    
    //重写构造方法
    init() {
    
        super.init(frame: CGRectZero)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置访客视图
    private func setupUI () {
    
        addSubview(circleView)
        addSubview(backView)
        addSubview(tipLable)
        addSubview(iconView)
        addSubview(LogInBut)
        addSubview(registerBut)
        //设置所有控件frame布局失效
        for v in subviews{
            v.translatesAutoresizingMaskIntoConstraints = false
 /// VFL布局
        /*
        view1.attr1 = view2.attr2 * multiplier + constant
        
        item: 要约束的对象
        attribute: 约束的属性  .CenterX 向水平中心点
        relatedBy:一般填 .Equal
        toItem:相对于那一个视图的约束
        attribute:乘积 一般真1.0
        constant:一般真1.0相对于当前点不变，可以调整大小改变位置
        */
       // NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0)
        //设置所有控件frame布局失效
//        for v in subviews{
//            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
        //圆形图标约束
        //设置circleView水平居中约束后，再添加到 当前view就是self上
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        //设置circleView垂直剧中
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -40))
        
        //大图标约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //文字约束
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 18))
        // 设置lable 宽度 toItem: nil, attribute: .NotAnAttribute, 没有约束参照对象视图，对应.NotAnAttribute属性
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 230))
        // 设置lable 高 toItem: nil, attribute: .NotAnAttribute, 没有约束参照对象视图，对应.NotAnAttribute属性
        // 当前字体14号，设置constant: 28表示高占两行
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60))
        
        
        //登陆按钮水平约束
        addConstraint(NSLayoutConstraint(item: LogInBut, attribute: .Left, relatedBy: .Equal, toItem: tipLable, attribute: .Left, multiplier: 1.0, constant: 0))
        //登陆按钮垂直约束
        addConstraint(NSLayoutConstraint(item: LogInBut, attribute: .Top, relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1.0, constant: 20))
        //登陆按钮宽约束
        addConstraint(NSLayoutConstraint(item: LogInBut, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        //登陆按钮高约束
        addConstraint(NSLayoutConstraint(item: LogInBut, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        //注册按钮水平约束
        addConstraint(NSLayoutConstraint(item: registerBut, attribute: .Right, relatedBy: .Equal, toItem: tipLable, attribute: .Right, multiplier: 1.0, constant: 0))
        //注册按钮垂直约束
        addConstraint(NSLayoutConstraint(item: registerBut, attribute: .Top, relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1.0, constant: 20))
        //注册按钮宽约束
        addConstraint(NSLayoutConstraint(item: registerBut, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        //注册按钮高约束
        addConstraint(NSLayoutConstraint(item: registerBut, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
 //// 背影视图约束
        //大图标约束
//        addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: backView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))

        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-60)-[registerBut]", options: [], metrics: nil, views: ["backView":backView,"registerBut":registerBut]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
        
        //为按钮注册点击事件
        LogInBut.addTarget(self, action: "LogInButClick", forControlEvents: .TouchUpInside)
        registerBut.addTarget(self, action: "registerButClick", forControlEvents: .TouchUpInside)
    }
    
    //懒加载所有控件
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示方案
    private lazy var tipLable:UILabel = {
    
        let l = UILabel()
    
        l.text = "回到这里看看有什么惊喜观注一些人回到这里看看有什么惊喜观注一些人回到这里看看有什么惊喜观注一些人"
        
        l.textAlignment = NSTextAlignment.Center
        
        l.font = UIFont.systemFontOfSize(14)
        
        l.textColor = UIColor.lightGrayColor()
        //自动换行
        l.numberOfLines = 0
        
        l.sizeToFit()
    
        return l
    }()
    
    //设置登录按钮
    private lazy var LogInBut: UIButton = {
        
        let but = UIButton()
        but.setTitle("登陆", forState: .Normal)
        but.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        
        but.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return but
    }()
    //设置注册按钮
    
    private lazy var registerBut: UIButton = {
    
        let but = UIButton()
        
        but.setTitle("注册", forState: .Normal)
        but.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        but.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return but
    }()

}
